package com.sysmind.ravi.margin.schedulerFiles;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.sysmind.ravi.margin.model.Stock;
import com.sysmind.ravi.margin.service.StockService;
import com.sysmind.ravi.margin.service.impl.StockServiceImpl;

public class GetGoogleStocksDataStoringInFile
{
	public static void main(String args[]) throws Exception
	{	 
	
		List<String> stocks = new ArrayList<String>();
		List <Integer> stocksType = new ArrayList<Integer>();
		
	 	stocks.add("GOOG"); 	stocksType.add(1);	//google
	 	stocks.add("MSFT"); 	stocksType.add(1);	//microsoft
	 	stocks.add("AAPL"); 	stocksType.add(1);	//apple
	 	stocks.add("INTC"); 	stocksType.add(1);	//intel
	 	stocks.add("IBM"); 		stocksType.add(1);	//ibm
	 	stocks.add("YHOO"); 	stocksType.add(1);	//amazon
	 	stocks.add("CSCO"); 	stocksType.add(1);	//cisco
	 	stocks.add("CTXS"); 	stocksType.add(1);	//Citrix
	 	stocks.add("DELL"); 	stocksType.add(1);	//dell
	 	stocks.add("NOVL"); 	stocksType.add(1);	//novell
	 	stocks.add("AMZN"); 	stocksType.add(1);	//yahoo
	 	
	 	Date currentDate = Calendar.getInstance().getTime();
	 	
	 	
	 	//System.out.println(stocks.size());
	 	String googleURL = "http://www.google.com/finance/info?client=ig&q=";
		
		//FileWriter fstream = new FileWriter("out.txt");
        //BufferedWriter out = new BufferedWriter(fstream);
	 	
	 	/*
	 	System.out.println(currentDate.toString());
	 	StringBuilder dateWrite = new StringBuilder(currentDate.toString());
	 	StringBuilder dateWriteFile = new StringBuilder();
	 	
	 	int i;
	 	
	 	i = dateWrite.indexOf(":");
	 	dateWriteFile.append(dateWrite.substring(0,i));
	 	dateWriteFile.append("_");
	 	//System.out.println(dateWriteFile.toString());
	 	
	 	dateWrite = dateWrite.delete(0,i+1);
	 	i = dateWrite.indexOf(":");
	 	dateWriteFile.append(dateWrite.substring(0,i));
	 	dateWriteFile.append("_");
	 	//System.out.println(dateWriteFile.toString());
	 	
	 	dateWrite = dateWrite.delete(0,i+1);
	 	dateWriteFile.append(dateWrite);
	 	//System.out.println(dateWriteFile.toString());
	 	
	 	String fileString = "stockData"+dateWriteFile.toString()+".txt";
	 	System.out.println(fileString);
	 	*/
	 	String fileString = "stockData.txt";
	 	FileOutputStream fout = new FileOutputStream (fileString);
	 	 
	    // Print a line of text
	    PrintStream p = new PrintStream(fout);
        
        Iterator <String> iterator1;
        Iterator <Integer> iterator2;
        try
        {
	        for(iterator1 = stocks.iterator(), iterator2 = stocksType.iterator(); iterator1.hasNext();)
			{	//p.print("hello");
				//System.out.println("hello");
				String completeURL = googleURL + iterator1.next().toString();
				//System.out.println(completeURL);
				
				URL yahoo = new URL(completeURL);
				URLConnection yc = yahoo.openConnection();
				BufferedReader in = new BufferedReader(
				                                new InputStreamReader(
				                                yc.getInputStream()));
				String inputLine;
				String writeLine="";			
				
				while ((inputLine = in.readLine()) != null) 
		        {   
		        	if(inputLine.indexOf(']')==-1)
		        	{
		        		writeLine = writeLine + inputLine;
		        	}
		        	else
		        	{
		        		//stockData.add(writeLine);
		        		
		        		//out.write(writeLine);
		        		//out.write("\n");
		        		int tempStockType = Integer.valueOf(iterator2.next());
		        		p.println(writeLine+" Stock Type "+tempStockType+" ]");
		        		//System.out.println(writeLine+" Stock Type "+tempStockType+" ]");
		        		//writeLine = "";
		        	}        		        		        	
		        }//End of while loop        
			}//End of For loop
        }
        catch(Exception e)
        {
        	System.out.println("Message : "+e.getMessage());
        }
        finally
        {
        	fout.close();
        	p.close();
        }
        
        ExtractStockInfofromFile esiff = new ExtractStockInfofromFile();
        List<Stock> stocksData = esiff.getStockList();
        StockService stockService = new StockServiceImpl();
        //System.out.println("sucess in inserting");
        
        stockService.setStockInfo(stocksData);
       // System.out.println(stockService.getStockInfo("goog"));
        //System.out.println(stockService.getStockSymbols());
	}
}
